package edu.gd.Test;

import edu.gd.utils.DBUtils;

import java.sql.*;

public class JDBCTest {
    //未封装
    public static void Test() throws ClassNotFoundException, SQLException {
        //注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        String url="jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";
        String USER="root";
        String PWD="123456";
        Connection connection=DriverManager.getConnection(url,USER,PWD);
        String sql="select * from ttt";
        PreparedStatement statement=connection.prepareCall(sql);
        ResultSet resultSet=statement.executeQuery();
        while (resultSet.next()){
            String user=resultSet.getString("user");
            String password=resultSet.getString("password");
            System.out.println(user+"  "+password);
        }
        resultSet.close();
        statement.close();
        connection.close();

    }
    //封装
    public static void Test2() throws SQLException {
        Connection connection= DBUtils.getConnection();
        String sql1="Insert IGNORE into ttt values(?,?)";
        PreparedStatement statement=connection.prepareCall(sql1);
        statement.setString(1,"a1");
        statement.setString(2,"123456");
        statement.executeUpdate();
        String sql="select * from ttt";
        statement=connection.prepareCall(sql);
        ResultSet resultSet=statement.executeQuery();
        System.out.println("--ID--PASSWORD---");
        while (resultSet.next()){
            String user=resultSet.getString("user");
            String password=resultSet.getString("password");
            System.out.println(user+"  "+password);
        }
        System.out.println("----------------");
        DBUtils.close(resultSet,statement,connection);

    }
    public static void main(String[]arg) throws SQLException, ClassNotFoundException {
        Test2();
    }
}
